/*
-----------------------------------------------------------------------------
This source file is part of FRAPPER
research.animationsinstitut.de
sourceforge.net/projects/frapper

Copyright (c) 2008-2009 Filmakademie Baden-Wuerttemberg, Institute of Animation 

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; version 2.1 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
-----------------------------------------------------------------------------
*/

//!
//! \file "ImageNode.h"
//! \brief Header file for ImageNode class.
//!
//! \author     Stefan Habel <stefan.habel@filmakademie.de>
//! \version    1.0
//! \date       30.06.2009 (last updated)
//!

#ifndef IMAGENODE_H
#define IMAGENODE_H

#include "FrapperPrerequisites.h"
#include "ViewNode.h"
#include <Ogre.h>

#if (OGRE_PLATFORM  == OGRE_PLATFORM_WIN32)
#include <windows.h>
#endif


//!
//! Base class for all nodes that produce image data.
//!
class FRAPPER_CORE_EXPORT ImageNode : public ViewNode
{

    Q_OBJECT

public: // static constants

    //!
    //! A constant value indicating that the number of images (to cache) is not
    //! known yet.
    //!
    static const int NumberUnknown = -1;

public: // constructors and destructors

    //!
    //! Constructor of the ImageNode class.
    //!
    //! \param name The name for the new node.
    //! \param parameterRoot A copy of the parameter tree specific for the type of the node.
    //! \param cacheEnabled Flag to enable or disable the image cache for this node.
    //! \param outputImageName The name of the geometry output parameter.
    //!
    ImageNode ( const QString &name, ParameterGroup *parameterRoot, bool cacheEnabled = true, const QString &outputImageName = "Image" );

    //!
    //! Destructor of the ImageNode class.
    //!
    //! Defined virtual to guarantee that the destructor of a derived class
    //! will be called if the instance of the derived class is saved in a
    //! variable of its parent class type.
    //!
    virtual ~ImageNode ();

public: // functions

    //!
    //! Returns the image that is generated by this node.
    //!
    //! \return The image that is generated by this node.
    //!
    virtual Ogre::TexturePtr getImage ();

    //!
    //! Returns whether a cached image is available for the frame with the
    //! given index.
    //!
    //! \param index The index of the frame for which to test whether a cached image is available.
    //! \return True if a cached image is available for the frame with the given index, otherwise False.
    //!
    bool isCached ( int index ) const;

    //!
    //! Returns the image cached for the frame with the given index.
    //!
    //! \param index The index of the frame for which to return the cached image.
    //! \return The cached image, or 0 if no cached image is available for the frame with the given index.
    //!
    Ogre::Image * getCachedImage ( int index ) const;

public slots: //

    //!
    //! Clears the image cache by freeing all memory allocated for the cache.
    //!
    void clearImageCache ();

protected: // functions

    //!
    //! Sets the number of images to cache to the given value.
    //!
    //! \param numberOfImagesToCache The number of images to cache.
    //!
    void setNumberOfImagesToCache ( unsigned int numberOfImagesToCache );

    //!
    //! Creates an image from the given texture.
    //!
    //! \param texturePointer The texture from which to copy the image data.
    //! \return The image created from the given texture, or 0 if the given texture is invalid.
    //!
    Ogre::Image * createImageFromTexture ( Ogre::TexturePtr texturePointer );

    //!
    //! Stores the given image in the cache for the frame with the given index.
    //!
    //! \param index The index of the frame for which to cache the image.
    //! \param image The image to cache for the frame with the given index.
    //!
    void cacheImage ( int index, Ogre::Image *image );

    //!
    //! Sets the given image as the output image in the output image parameter.
    //!
    //! \param image The image containing the pixel data to use for the texture.
    //!
    void setOutputImage ( Ogre::Image *image );

private: // functions

    //!
    //! Updates the cache status.
    //!
    void updateCacheStatus ();

protected: // data

    //!
    //! Flag that states whether the image cache for this node is enabled.
    //!
    bool m_cacheEnabled;

    //!
    //! Flag that states whether the cache is invalid and should be cleared.
    //!
    bool m_cacheInvalid;

    //!
    //! The name of the image output parameter.
    //!
    QString m_outputImageName;

    //!
    //! The total number of images to cache.
    //!
    int m_numberOfImagesToCache;

    //!
    //! The data structure to use for caching the loaded images.
    //!
    //! The key for the map is the index of a frame.
    //!
    QMap<int, Ogre::Image *> m_imageCache;

};


#endif
